//
//  MyCollectionViewLayOut.m
//  UIHomeWork15_网易标题自适应
//
//  Created by tusm on 16/8/3.
//  Copyright © 2016年 tusm. All rights reserved.
//

#import "MyCollectionViewLayOut.h"

@implementation MyCollectionViewLayOut
- (instancetype)init{

    self = [super init];
    if (self) {
        _contentWidth = 0;
        _attributesArray = [NSMutableArray array];
        _titleFont = [UIFont systemFontOfSize:17];
    }

    return self;
}

//必须实现的三个方法
- (void)prepareLayout{
     _itemWidth = 0;
    //起始位置
    CGFloat x = 0;
//    if (_attributesArray.count != 0) {
//        [_attributesArray removeAllObjects];
//    }
    for (int i = 0; i < [self.collectionView numberOfItemsInSection:0]; i++) {
        
        NSIndexPath *indexPath = [NSIndexPath indexPathForItem:i inSection:0];
        
        UICollectionViewLayoutAttributes *layoutAttributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
        
        x  = x + _itemWidth;
        
        _itemWidth = [self.delegate collectionView:self.collectionView layout:self widthForItemAtIndexPath:indexPath] + 5;
        
        CGFloat y = 0;
        
        CGFloat height = 44;
        
        self.contentWidth = x + _itemWidth;
        
        layoutAttributes.frame = CGRectMake(x, y, _itemWidth, height);
        
        [_attributesArray addObject:layoutAttributes];
    }

}

- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{

    return _attributesArray;
}

- (CGSize)collectionViewContentSize{

    return CGSizeMake(_contentWidth, 0);

}
@end
